using UnityEngine;
using UnityEngine.UI;

public class Minimap : MonoBehaviour
{
	public bool FixedPlayer;

	public Vector2 WorldRealSize;

	public RectTransform MiniMap;

	public GameObject Player;

	public RectTransform SafeArea;

	public RectTransform DeadArea;

	public RectTransform PlayerIndicator;

	public Text TimeText;

	public float MapScale = 1f;

	public void Zoom(float zoom)
	{
		if (!(MiniMap == null))
		{
			MapScale += zoom;
			if (MapScale < 1f)
			{
				MiniMap.anchoredPosition = Vector2.zero;
				MapScale = 1f;
			}
			MiniMap.localScale = Vector2.one * MapScale;
		}
	}

	private Vector2 WorldPositionToMap(Vector2 minimap, Vector3 realposition)
	{
		Vector2 zero = Vector2.zero;
		float num = resize(minimap.x, WorldRealSize.x);
		float num2 = resize(minimap.y, WorldRealSize.y);
		return new Vector2(realposition.x * num, realposition.z * num2);
	}

	private float resize(float minimap, float realsize)
	{
		return minimap / realsize;
	}

	private void OnEnable()
	{
		if (UnitZ.levelManager == null)
		{
			return;
		}
		LevelPreset currentLevel = UnitZ.levelManager.GetCurrentLevel();
		if (currentLevel != null)
		{
			if (MiniMap != null && MiniMap.GetComponent<Image>() != null)
			{
				MiniMap.GetComponent<Image>().sprite = currentLevel.Minimap;
			}
			WorldRealSize = new Vector2(currentLevel.MinimapSize, currentLevel.MinimapSize);
		}
	}

	private void Start()
	{
	}

	private void Update()
	{
		if (!(UnitZ.NetworkGameplay != null))
		{
			return;
		}
		BattleRoyaleGamePlay component = UnitZ.NetworkGameplay.GetComponent<BattleRoyaleGamePlay>();
		if (component != null)
		{
			if (TimeText != null)
			{
				float getTimeToNextArea = component.GetTimeToNextArea;
				float num = Mathf.Floor(getTimeToNextArea / 60f);
				float num2 = Mathf.RoundToInt(getTimeToNextArea % 60f);
				TimeText.text = num + ":" + num2;
			}
			if ((bool)SafeArea)
			{
				if (component.safeArea != null)
				{
					SafeArea.anchoredPosition = WorldPositionToMap(new Vector2(MiniMap.rect.width, MiniMap.rect.height), component.safeArea.transform.position);
					float num3 = resize(MiniMap.rect.width, WorldRealSize.x);
					Vector3 localScale = component.safeArea.transform.localScale;
					float x = num3 * localScale.x;
					float num4 = resize(MiniMap.rect.height, WorldRealSize.y);
					Vector3 localScale2 = component.safeArea.transform.localScale;
					Vector2 sizeDelta = new Vector2(x, num4 * localScale2.z);
					SafeArea.sizeDelta = sizeDelta;
				}
				SafeArea.gameObject.SetActive((bool)component.safeArea && component.safeArea.activeSelf);
			}
			if ((bool)DeadArea)
			{
				if (component.lastDeadArea != null)
				{
					DeadArea.anchoredPosition = WorldPositionToMap(new Vector2(MiniMap.rect.width, MiniMap.rect.height), component.lastDeadArea.transform.position);
					float num5 = resize(MiniMap.rect.width, WorldRealSize.x);
					Vector3 localScale3 = component.lastDeadArea.transform.localScale;
					float x2 = num5 * localScale3.x;
					float num6 = resize(MiniMap.rect.height, WorldRealSize.y);
					Vector3 localScale4 = component.lastDeadArea.transform.localScale;
					Vector2 sizeDelta2 = new Vector2(x2, num6 * localScale4.z);
					DeadArea.sizeDelta = sizeDelta2;
				}
				DeadArea.gameObject.SetActive((bool)component.lastDeadArea && component.lastDeadArea.activeSelf);
			}
		}
		else
		{
			if ((bool)SafeArea)
			{
				SafeArea.gameObject.SetActive(value: false);
			}
			if ((bool)DeadArea)
			{
				DeadArea.gameObject.SetActive(value: false);
			}
		}
		if (Player == null && (bool)UnitZ.playerManager && (bool)UnitZ.playerManager.PlayingCharacter)
		{
			Player = UnitZ.playerManager.PlayingCharacter.gameObject;
		}
		if (MiniMap != null && PlayerIndicator != null && Player != null)
		{
			if (!FixedPlayer)
			{
				Vector2 anchoredPosition = WorldPositionToMap(new Vector2(MiniMap.rect.width, MiniMap.rect.height), Player.transform.position);
				PlayerIndicator.anchoredPosition = anchoredPosition;
				Quaternion rotation = PlayerIndicator.rotation;
				Vector3 eulerAngles = rotation.eulerAngles;
				float x3 = eulerAngles.x;
				Vector3 eulerAngles2 = rotation.eulerAngles;
				float y = eulerAngles2.y;
				Vector3 eulerAngles3 = Player.transform.rotation.eulerAngles;
				rotation.eulerAngles = new Vector3(x3, y, 0f - eulerAngles3.y);
				PlayerIndicator.rotation = rotation;
			}
			else
			{
				MiniMap.anchoredPosition = WorldPositionToMap(new Vector2(0f - MiniMap.rect.width, 0f - MiniMap.rect.height), Player.transform.position);
				PlayerIndicator.anchoredPosition = Vector2.zero;
				Quaternion rotation2 = PlayerIndicator.rotation;
				Vector3 eulerAngles4 = rotation2.eulerAngles;
				float x4 = eulerAngles4.x;
				Vector3 eulerAngles5 = rotation2.eulerAngles;
				float y2 = eulerAngles5.y;
				Vector3 eulerAngles6 = Player.transform.rotation.eulerAngles;
				rotation2.eulerAngles = new Vector3(x4, y2, 0f - eulerAngles6.y);
				PlayerIndicator.rotation = rotation2;
			}
		}
	}
}
